//
//  OPEatDetailController.m
//  OnePiece
//
//  Created by wsy on 15/10/28.
//  Copyright © 2015年 wsy. All rights reserved.
//

#import "OPEatDetailController.h"
#import "OPEatDetailHeadView.h"
#import "OPEatDetailWithImgCell.h"
#import "OPEatDetailNoImgCell.h"
#import "OPRequesterManager.h"
#import "UMSocial.h"
#import <UIImageView+WebCache.h>
@interface OPEatDetailController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, strong) NSMutableArray *dataSourceArr;
@property (nonatomic, strong) UIImageView *shareimg;
@property (nonatomic, strong) OPEatDetailMainModel *model;
@end

@implementation OPEatDetailController
- (NSMutableArray *)dataSourceArr{
    if (_dataSourceArr == nil) {
        _dataSourceArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArr;
}
-(void)configureData{
    NSString *str1 = @"http://www.youyuwei.com/api/note?app_code=com.youyuwei.yuwei.ios&hardware_model=%2C2&app_ver=2.1&sys_ver=&device_type=ios&ywsdk_ver=20131018&open_udid=&start=0&oauth_timestamp=&device_token=&x_auth_mode=client_auth&oauth_signature_method=HMAC-SHA1&id=";
    NSString *str2 = @"&ver=2&idfa=&channel_id=App%20Store&oauth_version=1.0&oauth_consumer_key=4&oauth_token=&oauth_signature=%3D&debug=0&oauth_nonce=";
    NSString *urlstr = [NSString stringWithFormat:@"%@%@%@",str1,self.eatid,str2];
    [OPRequesterManager requestWithType:GET urlString:urlstr parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        _model = [[OPEatDetailMainModel alloc]init];
        [_model setValuesForKeysWithDictionary:dic[@"data"]];
        OPEatDetailHeadView *headView = [[OPEatDetailHeadView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH , SCREEN_WIDTH*235/600+90)];
        _mainTableView.tableHeaderView = headView;
        [headView setConfigureModel:_model];
        _shareimg = [[UIImageView alloc]init];
        [_shareimg sd_setImageWithURL:[NSURL URLWithString:_model.cover]];
        NSArray *array = dic[@"data"][@"content"];
        for (NSDictionary *dict in array) {
            OPEatDetailArrayModel *m = [[OPEatDetailArrayModel alloc]init];
            [m setValuesForKeysWithDictionary:dict];
            [self.dataSourceArr addObject:m];
        }
        [_mainTableView reloadData];
    } error:^(NSError *error) {
        NSLog(@"美食详情请求错误：%@",[error localizedDescription]);
    }];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    _mainTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    _mainTableView.backgroundColor = [UIColor colorWithRed:1.000 green:0.985 blue:0.971 alpha:1.000];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    _mainTableView.showsVerticalScrollIndicator = NO;
    _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_mainTableView registerClass:[OPEatDetailNoImgCell class] forCellReuseIdentifier:@"text"];
    [_mainTableView registerClass:[OPEatDetailWithImgCell class] forCellReuseIdentifier:@"img"];
    [self.view addSubview:_mainTableView];
    [self configureData];
    
    UIButton *backButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    backButton.frame = CGRectMake(10, 25, 30, 30);
    backButton.alpha = 0.5;
    [backButton setImage:[UIImage imageNamed:@"meishi-back"] forState:(UIControlStateNormal)];
    [backButton addTarget:self action:@selector(back:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:backButton];
    
    UIButton *shareButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    shareButton.frame = CGRectMake(SCREEN_WIDTH - 45, 27, 25, 25);
    shareButton.alpha = 0.5;
    [shareButton setImage:[UIImage imageNamed:@"meishi-share"] forState:(UIControlStateNormal)];
    [shareButton addTarget:self action:@selector(share) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:shareButton];
}
- (void)share
{
    
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"562dcfa4e0f55a2a14004075"
                                      shareText:_model.title
                                     shareImage:_shareimg.image
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToRenren,UMShareToDouban,UMShareToFacebook,UMShareToTwitter,nil]
                                       delegate:nil];
}
- (void)back:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OPEatDetailArrayModel *model = _dataSourceArr[indexPath.row];
    if ([model.type isEqualToString:@"text"]) {
        OPEatDetailNoImgCell *cell = [tableView dequeueReusableCellWithIdentifier:@"text" forIndexPath:indexPath];
        [cell setConfigureModel:model];
        return cell;
    }else{
        OPEatDetailWithImgCell *cell = [tableView dequeueReusableCellWithIdentifier:@"img" forIndexPath:indexPath];
        [cell setConfigureModel:model];
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OPEatDetailArrayModel *model = _dataSourceArr[indexPath.row];
    if ([model.type isEqualToString:@"text"]) {
        return [OPEatDetailNoImgCell heightForCell:model];
    }else{
        return [OPEatDetailWithImgCell heightForCell:model];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
